//
//  NewWebViewController.m
//  svqj
//
//  Created by xftt on 14-6-30.
//
//

#import "NewWebViewController.h"
#import "MainViewController.h"

@interface NewWebViewController ()<UIWebViewDelegate>
{
    UIWebView *_vWeb;
    UIView *_showView;
    UILabel *_lbTitle;
    
    UIView *vbg;
    UIView *vloading;
}

@end

@implementation NewWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSInteger barheight = 0;
    if([UIDevice currentDevice].systemVersion.floatValue >= 7.0)
    {
        barheight = 20;
    }
    
    _showView = [[UIView alloc]initWithFrame:CGRectMake(0, barheight, 1024, 748)];
    [self.view addSubview:_showView];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 50)];
    [titleView setBackgroundColor:[UIColor colorWithHexString:@"#343434"]];
    [_showView addSubview:titleView];
    
    _lbTitle = [[UILabel alloc]initWithFrame:titleView.frame];
    [_lbTitle setTextColor:[UIColor whiteColor]];
    [_lbTitle setBackgroundColor:[UIColor clearColor]];
    [_lbTitle setTextAlignment:NSTextAlignmentCenter];
    [_showView addSubview:_lbTitle];
    
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setFrame:CGRectMake(0, 0, 70, 50)];
    [btnBack setTitle:@"返回" forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(doClose:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:btnBack];
    
    _vWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, 1024, 698)];
    [_vWeb.scrollView setBounces:NO];
    [_showView addSubview:_vWeb];
    _vWeb.delegate = self;
    
    vbg = [[UIView alloc] initWithFrame:_vWeb.frame];
//    [_showView addSubview:vbg];
    [vbg setBackgroundColor:[UIColor blackColor]];
    [vbg setAlpha:0.3];
    
    vloading = [[UIView alloc] initWithFrame:CGRectMake(460, 284, 100, 100)];
    [vloading setBackgroundColor:[UIColor colorWithHexString:@"#343434"]];
    vloading.layer.cornerRadius = 15;
//    [_showView addSubview:vloading];
    
    UIActivityIndicatorView *actv = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(40, 28, 20, 20)];
    [actv setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [actv setHidden:NO];
    [actv startAnimating];
    [vloading addSubview:actv];
    
    UILabel *lbLoading = [[UILabel alloc]initWithFrame:CGRectMake(9, 58, 82, 20)];
    [lbLoading setBackgroundColor:[UIColor clearColor]];
    [lbLoading setTextAlignment:NSTextAlignmentCenter];
    [lbLoading setTextColor:[UIColor colorWithHexString:@"AAAAAA"]];
    [lbLoading setText:@"加载中..."];
    [lbLoading setFont:[UIFont systemFontOfSize:15]];
    [vloading addSubview:lbLoading];
    
    if ([_url length]>0) {
        [self setUrl:_url];
    }
}

-(void)doClose:(UIButton*)sender
{
//    [self dismissViewControllerAnimated:NO completion:nil];
    [_vWeb stopLoading];
    if (_vWeb != nil) {
        [_vWeb setDelegate:nil];
        _vWeb = nil;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setUrl:(NSString *)url
{
    _url = url;
    [_vWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}

-(void)setUrlStr:(NSString *)urlStr
{
    [_showView setFrameY:0];
    [self setUrl:urlStr];
}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeOther ||
        navigationType == UIWebViewNavigationTypeLinkClicked)//点击链接
    {
        NSURL *url = [request URL];
        if ([_url isEqualToString:url.absoluteString]) {
            return YES;
        }
        NSString *query = [url query];
        NSLog(@"url %@ \n %@",url, url.absoluteString);
		if (query && [query rangeOfString:@"openwin=1"].location != NSNotFound)
        {
            [self performSelector:@selector(openURLInNewWebView:) withObject:url.absoluteString afterDelay:0.5];
            return NO;
        }
    }
    
    [vloading setHidden:NO];
    [vbg setHidden:NO];
	return YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
    [vloading setHidden:YES];
    [vbg setHidden:YES];
    [_lbTitle setText:[webView stringByEvaluatingJavaScriptFromString:@"document.title"]];
}

- (void)openURLInNewWebView:(NSString *)url
{
    MainViewController *main = (MainViewController*)self.tabFrameController;
    if (main == nil) {
        NewWebViewController *new = [[NewWebViewController alloc] initWithNibName:nil bundle:nil];
        [self presentViewController:new animated:YES completion:nil];
        [new setUrl:url];
    }
    else{
        [main showNewWeb:url];
    }
}


@end
